﻿using System;
using System.Collections.Generic;
using System.Linq;
using Kugar.Core.ExtMethod;
using Kugar.Core.Web;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    public  class PageCommon_PanelBoxContainer:ContainerBase
    {
        private ColorPaletteEnum _styleColor = ColorPaletteEnum.@default;
        private bool _noborder = false;
        private bool _isRemovable = false;
        private bool _isCollapsable = false;
        private string _title = "";
        private string _removeTips = "";
        private string _bodyHeight = "";
        private string _bodyMaxHeight = "";
        private Lazy<List<ButtonConfigBase>> _footerBtnLst=new Lazy<List<ButtonConfigBase>>();
        private int _oldLabelWidth = -1;
        private string _oldControlWidth = "";
        private List<ButtonConfigBase> _titleBtns = null;

        public PageCommon_PanelBoxContainer(ViewContext viewContext) : base(viewContext)
        {
            this.Css("box webui-ctrl-panelbox");

            this._oldLabelWidth = ((int?)ViewContext.ViewData["DefaultLabelWidth"]) ?? 2;
            this._oldControlWidth = (string)ViewContext.ViewData["DefaultControlWidth"];
        }

        
        public PageCommon_PanelBoxContainer Title(string title)
        {
            _title = title;

            return this;
        }

        public PageCommon_PanelBoxContainer Color(ColorPaletteEnum color)
        {
            _styleColor = color;

            return this;
        }

        public PageCommon_PanelBoxContainer NoBorder(bool noborder = true)
        {
            _noborder = noborder;

            return this;
        }

        public PageCommon_PanelBoxContainer Collapsable(bool isCollapsable = true)
        {
            _isCollapsable = isCollapsable;

            return this;
        }

        public PageCommon_PanelBoxContainer BodyHeight(int height)
        {
            _bodyHeight = $"{height}px";

            return this;
        }

        public PageCommon_PanelBoxContainer BodyMaxHeight(int maxHeight)
        {
            _bodyMaxHeight = $"{maxHeight}px";

            return this;
        }

        public PageCommon_PanelBoxContainer Removable(bool isRemovable = true, string tips = "")
        {
            _isRemovable = isRemovable;
            _removeTips = tips;

            return this;
        }

        public PageCommon_PanelBoxContainer AddFooterButton(Action<ButtonConfigBase> config)
        {
            var btn = new ButtonConfigBase(ViewContext);

            btn.Css("btn btn-flat");

            config(btn);

            _footerBtnLst.Value.Add(btn);

            return this;
        }

        /// <summary>
        ///添加一个右侧的标题栏按钮
        /// </summary>
        /// <param name="configFunc"></param>
        /// <returns></returns>
        public PageCommon_PanelBoxContainer AddTitleButton(Action<ButtonConfigBase> configFunc)
        {
            var config=new ButtonConfigBase(ViewContext);
            config.Attributes.Class("btn btn-box-tool");

            configFunc?.Invoke(config);

            _titleBtns = _titleBtns ?? new List<ButtonConfigBase>();

            _titleBtns.Add(config);

            return this;
        }

        /// <summary>
        /// 控件范围内,FormField的 默认标题宽度
        /// </summary>
        /// <param name="labelWidth"></param>
        /// <returns></returns>
        public PageCommon_PanelBoxContainer DefaultLabelWidth(int labelWidth)
        {
            ViewContext.ViewData["DefaultLabelWidth"] = labelWidth;

            return this;
        }

        /// <summary>
        /// 控件范围内,FormField的默认内控件宽度
        /// </summary>
        /// <param name="controlWidth"></param>
        /// <returns></returns>
        public PageCommon_PanelBoxContainer DefaultControlWidth(string controlWidth)
        {
            ViewContext.ViewData["DefaultControlWidth"] = controlWidth;

            return this;
        }

        internal override ContainerBase RenderInternal()
        {
            /*
              <div class="box box-default">
                <div class="box-header with-border">
                  <h3 class="box-title">Progress Bars Different Sizes</h3>
                </div>
                <!-- /.box-header -->
                <div class="box-body">
              
                </div>
                <!-- /.box-body -->
              </div>
             */
             
            this.Css("box-" + _styleColor.ToString());

            if (!_noborder)
            {
                this.Css("box-solid");
            }
            else
            {
                this.Css("box-noborder");
            }

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            if (!string.IsNullOrWhiteSpace(_title) || _isRemovable || _isCollapsable || _titleBtns.HasData())
            {
                ViewContext.Writer.WriteLine($"<div class=\"box-header {_noborder.If("", "with-border")}\" style=\"min-height:37px;\">");

                ViewContext.Writer.WriteLine(_title);

                if (_isRemovable || _isCollapsable || _titleBtns.HasData())
                {
                    ViewContext.Writer.Write("<div class=\"box-tools pull-right\">");

                    if (_titleBtns.HasData())
                    {
                        foreach (var btn in _titleBtns)
                        {
                            if (btn._hide)
                            {
                                continue;
                            }

                            btn.Css("btn-" + btn.GetColor);
                            btn.Command?.RenderToButton(btn);

                            ViewContext.Writer.Write($"<button {btn.Attributes}>{btn.GetText}</button>");
                        }                        
                    }


                    if (_isCollapsable)
                    {
                        ViewContext.Writer.Write("<button type=\"button\" class=\"btn btn-box-tool\" data-widget=\"collapse\"><i class=\"fa fa-minus\"></i>\r\n</button>");
                    }

                    if (_isRemovable)
                    {
                        ViewContext.Writer.Write($"<button type=\"button\" class=\"btn btn-box-tool\" data-widget=\"remove-confirm\" data-tips=\"{_removeTips}\"><i class=\"fa fa-times\"></i></button>");
                        ViewContext.Writer.Write("<button type=\"button\" style=\"display:none;\" data-widget=\"remove\"></button>");
                    }

                    ViewContext.Writer.Write("</div> <!-- /.box-tools -->");
                }

                ViewContext.Writer.WriteLine("</div> <!-- /.box-header -->");
            }

            ViewContext.Writer.WriteLine($"<div class=\"box-body\" style=\"{(string.IsNullOrWhiteSpace(_bodyHeight) ? "" : $"height:{_bodyHeight};")}{(string.IsNullOrWhiteSpace(_bodyMaxHeight) ? "" : $"max-height:{_bodyMaxHeight};")}\">");

            return this;
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

            ViewContext.Writer.WriteLine("</div> <!-- /.box-body -->");

            if (_footerBtnLst.IsValueCreated && _footerBtnLst.Value.Where(x=>!x._hide).HasData())
            {
                ViewContext.Writer.Write("<div class=\"box-footer\">");

                ViewContext.Writer.Write("<div class=\"input-group\" >");

                ViewContext.Writer.Write("<span class=\"input-group-btn\">");

                foreach (var btn in _footerBtnLst.Value)
                {
                    if (btn._hide)
                    {
                        continue;
                    }

                    btn.Css("btn-" + btn.GetColor);
                    btn.Command?.RenderToButton(btn);
                        

                    ViewContext.Writer.Write($"<button {btn.Attributes}>{btn.GetText}</button>");
                }

                ViewContext.Writer.Write("</span>");

                ViewContext.Writer.Write("</div>");

                ViewContext.Writer.Write("</div>");
            }

            ViewContext.Writer.WriteLine("</div>");

            ViewContext.ViewData["DefaultLabelWidth"] = _oldLabelWidth;
            ViewContext.ViewData["DefaultControlWidth"]= _oldControlWidth;

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }
        }
 
    }


    public class PageCommon_PanelBox : ControlBase
    {
        private ColorPaletteEnum _styleColor = ColorPaletteEnum.@default;
        private bool _noborder = false;
        private bool _isRemovable = false;
        private bool _isCollapsable = false;
        private string _title = "";
        private string _removeTips = "";
        private string _bodyHeight = "";
        private string _bodyMaxHeight = "";
        private Lazy<List<ButtonConfigBase>> _footerBtnLst = new Lazy<List<ButtonConfigBase>>();
        private string  _content = null;
        private ControlBase _ctrl = null;
        private int _oldLabelWidth = -1;
        private string _oldControlWidth = "";

        public PageCommon_PanelBox(ViewContext viewContext) : base(viewContext)
        {
            this.Css("box webui-ctrl-panelbox");

            this._oldLabelWidth = ((int?)ViewContext.ViewData["DefaultLabelWidth"]) ?? 2;
            this._oldControlWidth = (string)ViewContext.ViewData["DefaultControlWidth"];
        }

        public PageCommon_PanelBox Title(string title)
        {
            _title = title;

            return this;
        }

        public PageCommon_PanelBox Title(Func<PageCommon_PanelBox, HelperResult> title)
        {
            _title = title(this).ToHtmlString();

            return this;
        }

        public PageCommon_PanelBox Content(Func<PageCommon_PanelBox, HelperResult> content)
        {
            _content = content(this).ToHtmlString();
            _ctrl = null;

            return this;
        }

        /// <summary>
        /// 直接包含一个控件
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public PageCommon_PanelBox Content(ControlBase control)
        {
            _ctrl = control;
            _content = "";

            return this;
        }
        
        public PageCommon_PanelBox Color(ColorPaletteEnum color)
        {
            _styleColor = color;

            return this;
        }

        public PageCommon_PanelBox NoBorder(bool noborder = true)
        {
            _noborder = noborder;

            return this;
        }

        public PageCommon_PanelBox Collapsable(bool isCollapsable = true)
        {
            _isCollapsable = isCollapsable;

            return this;
        }

        public PageCommon_PanelBox BodyHeight(int height)
        {
            _bodyHeight = $"{height}px";

            return this;
        }

        public PageCommon_PanelBox BodyMaxHeight(int maxHeight)
        {
            _bodyMaxHeight = $"{maxHeight}px";

            return this;
        }

        public PageCommon_PanelBox Removable(bool isRemovable = true, string tips = "")
        {
            _isRemovable = isRemovable;
            _removeTips = tips;

            return this;
        }

        public PageCommon_PanelBox DefaultLabelWidth(int labelWidth)
        {
            ViewContext.ViewData["DefaultLabelWidth"] = labelWidth;

            return this;
        }

        public PageCommon_PanelBox DefaultControlWidth(string controlWidth)
        {
            ViewContext.ViewData["DefaultControlWidth"] = controlWidth;

            return this;
        }

        public PageCommon_PanelBox AddFooterButton(Action<ButtonConfigBase> config)
        {
            var btn = new ButtonConfigBase(ViewContext);

            btn.Css("btn btn-flat");

            config(btn);

            _footerBtnLst.Value.Add(btn);

            return this;
        }

        public override HtmlString Render()
        {
            this.Css("box-" + _styleColor.ToString());

            if (!_noborder)
            {
                this.Css("box-solid");
            }
            else
            {
                this.Css("box-noborder");
            }

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            if (!string.IsNullOrWhiteSpace(_title) || _isRemovable || _isCollapsable)
            {
                ViewContext.Writer.WriteLine($"<div class=\"box-header {_noborder.If("", "with-border")}\" style=\"min-height:37px;\">");

                ViewContext.Writer.WriteLine(_title);

                if (_isRemovable || _isCollapsable)
                {
                    ViewContext.Writer.Write("<div class=\"box-tools pull-right\">");

                    if (_isCollapsable)
                    {
                        ViewContext.Writer.Write("<button type=\"button\" class=\"btn btn-box-tool\" data-widget=\"collapse\"><i class=\"fa fa-minus\"></i>\r\n</button>");
                    }

                    if (_isRemovable)
                    {
                        ViewContext.Writer.Write($"<button type=\"button\" class=\"btn btn-box-tool\" data-widget=\"remove-confirm\" data-tips=\"{_removeTips}\"><i class=\"fa fa-times\"></i></button>");
                        ViewContext.Writer.Write("<button type=\"button\" style=\"display:none;\" data-widget=\"remove\"></button>");
                    }


                    ViewContext.Writer.Write("</div> <!-- /.box-tools -->");
                }

                ViewContext.Writer.WriteLine("</div> <!-- /.box-header -->");
            }

            ViewContext.Writer.WriteLine($"<div class=\"box-body\" style=\"{(string.IsNullOrWhiteSpace(_bodyHeight) ? "" : $"height:{_bodyHeight};")}{(string.IsNullOrWhiteSpace(_bodyMaxHeight) ? "" : $"max-height:{_bodyMaxHeight};")}\">");

            if (_ctrl != null)
            {
                _ctrl.Render();
            }
            else
            {
                ViewContext.Writer.WriteLine(_content);
            }

            ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

            ViewContext.Writer.WriteLine("</div> <!-- /.box-body -->");

            if (_footerBtnLst.IsValueCreated && _footerBtnLst.Value.Where(x => !x._hide).HasData())
            {
                ViewContext.Writer.Write("<div class=\"box-footer\">");

                ViewContext.Writer.Write("<div class=\"input-group\" >");

                ViewContext.Writer.Write("<span class=\"input-group-btn\">");

                foreach (var btn in _footerBtnLst.Value)
                {
                    if (btn._hide)
                    {
                        continue;
                    }

                    btn.Css("btn-" + btn.GetColor);
                    btn.Command?.RenderToButton(btn);


                    ViewContext.Writer.Write($"<button {btn.Attributes}>{btn.GetText}</button>");
                }

                ViewContext.Writer.Write("</span>");

                ViewContext.Writer.Write("</div>");

                ViewContext.Writer.Write("</div>");
            }

            ViewContext.Writer.WriteLine("</div>");

            ViewContext.ViewData["DefaultLabelWidth"] = _oldLabelWidth;
            ViewContext.ViewData["DefaultControlWidth"] = _oldControlWidth;

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }

            return HtmlString.Empty;
        }


    }
}
